<?php
require_once(getcwd().'/application/models/SS_model.php');

class Leader_model extends SS_model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->library('session');
		$this->load->library('encryption');
	}

	public function getSubWorkerState($leader_id)
	{
		$sql = "SELECT profile.user_id,profile.user_name , profile.title FROM leader LEFT OUTER JOIN profile ON leader.user_id = profile.user_id WHERE leader.leader_id = ? ";
		return $this->db->query($sql,array($leader_id))->result_array();
	}

	public function getLeaveApplyById($page = 0,$wokrer_id = 0)
	{
		if(!is_numeric($page))
		{
			return array('code'=>'101' ,'content' => 'Page Not Exist');
		}
		
		$pageSize  = 10;
		$start = $page - 3;
		$end = $page + 3;
		$res = array();
		if(!is_numeric($wokrer_id))
		{
			return array('code'=>'101' ,'content' => 'Worker Not Exist');
		}
		//获取总数
		$sql = 'SELECT COUNT(auto_id) as num FROM `leave_apply`  LEFT OUTER JOIN leader ON leader.user_id = leave_apply.applier_id WHERE  applier_id = ? AND leader.leader_id = ? AND state = "pending"';
		$result = $this->db->query($sql,array($wokrer_id,$_SESSION['user_id']))->row_array();
		$page_sum = isset($result['num']) ? $result['num'] : 0;
		
		
		if($page_sum/$pageSize < $page)
		{
			return array('code'=>'102' ,'content' => 'Page Not Exist');
		}
		
		
		if($start < 0 ) $start = 0;
		if($end > $page_sum/$pageSize ) $end = floor($page_sum/$pageSize);

		if($end == $start) 
		{
			$res['pages'] = array(0);
		}
		else 
		{
			$res['pages'] = range($start,$end);	
		}
		$sql = 'SELECT leave_apply.auto_id,leave_apply.applier_id,create_time,start_time,end_time,time_length,state,leave_type FROM leave_apply LEFT OUTER JOIN leader ON leader.user_id = leave_apply.applier_id WHERE leave_apply.applier_id = ? AND state = ? AND leader.leader_id = ? ORDER BY create_time DESC LIMIT ?,?;';
		$result = $this->db->query($sql,array($wokrer_id,"pending",$_SESSION['user_id'],$pageSize*$page,$pageSize))->result_array();
		$res['data'] = $result;
		$res['page'] = $page;
		$res['all'] = floor($page_sum/$pageSize);
		return $res;
	}

	public function getLeaveApplyAll($page = 0)
	{
		if(!is_numeric($page))
		{
			return array('code'=>'101' ,'content' => 'Page Not Exist');
		}
		
		$pageSize  = 10;
		$start = $page - 3;
		$end = $page + 3;
		$res = array();
		
		
		//获取总数
		$sql = 'SELECT COUNT(leave_apply.auto_id) as num FROM `leave_apply` LEFT OUTER JOIN leader ON leader.user_id = leave_apply.applier_id WHERE leader.leader_id = ?  AND state = "pending"';
		$result = $this->db->query($sql,array($_SESSION['user_id']))->row_array();
		$page_sum = isset($result['num']) ? $result['num'] : 0;
		
		
		if($page_sum/$pageSize < $page)
		{
			return array('code'=>'102' ,'content' => 'Page Not Exist');
		}
		
		
		if($start < 0 ) $start = 0;
		if($end > $page_sum/$pageSize ) $end = floor($page_sum/$pageSize);

		if($end == $start) 
		{
			$res['pages'] = array(0);
		}
		else 
		{
			$res['pages'] = range($start,$end);	
		}
		$sql = 'SELECT leave_apply.auto_id,leave_apply.applier_id,create_time,start_time,profile.user_name,end_time,state,time_length,leave_type FROM `leave_apply` LEFT OUTER JOIN leader ON leader.user_id = leave_apply.applier_id LEFT OUTER JOIN profile ON leader.user_id = profile.user_id WHERE leader.leader_id = ? AND state = ?  ORDER BY create_time DESC LIMIT ?,?;';
		$result = $this->db->query($sql,array($_SESSION['user_id'],"pending",$pageSize*$page,$pageSize))->result_array();
		$res['data'] = $result;
		$res['page'] = $page;
		$res['all'] = floor($page_sum/$pageSize);
		return $res;
	}

	public function passLeaveState($data)
	{
		$sql = "UPDATE leave_apply SET state = ? WHERE auto_id = ? ;";
		$this->db->query($sql,array("approved_by_mgr",$data['id']));
		return array("code"=>"200");
	}
}